package com.engine.zhlx.crm.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.zhlx.crm.utils.CrmUtil;
import weaver.general.BaseBean;
import weaver.general.StringUtil;
import weaver.general.Util;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-12-30
 * @Description: Sap主动推送信息
 * @ModifyLog:
 **/

public class WeaCrmPushDataWeb extends BaseBean {
    /**
     * @Description: CRM商机信息推送
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023-09-10
     */
    @POST
    @Path("/saveBOInfoData")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String saveBOInfoData(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        CrmUtil crmUtil = new CrmUtil();
        String code = "SUCCESS";
        String message = "调用成功";
        String tablename = "uf_ECjrsj";
        JSONObject data = new JSONObject();
        try {
            writeLog(">>>>>createBOInfoData>>>paramsJson=" + paramsJson);
            String dataStr = paramsJson.getString("data");
            if (!crmUtil.isJSONObject(dataStr)) {
                code = "PARAMETER_ERROR";
                message = "data请求参数格式错误";
            } else {
                JSONObject dataJson = JSONObject.parseObject(dataStr);
                String type = dataJson.getString("type");
                String dataList = dataJson.getString("dataList");
                if (!crmUtil.isJSONArray(dataList)) {
                    code = "PARAMETER_ERROR";
                    message = "dataList请求参数格式错误";
                } else {
                    if ("1".equals(type)) {//创建
                        JSONArray dataArray = JSONArray.parseArray(dataList);
                        int counts = dataArray.size();
                        int count = 0;
                        for (int i = 0; i < dataArray.size(); i++) {
                            Map<String, Object> map = new HashMap<>();
                            JSONObject jsonObject = dataArray.getJSONObject(i);
                            for (Object key : jsonObject.keySet()) {
                                map.put(Util.null2String(key), jsonObject.get(key));
                            }
                            boolean flag = crmUtil.insertModelData(tablename, map);
                            if (flag) {
                                count++;
                            }
                        }
                        data.put("totalcount", counts);
                        data.put("succcount", count);
                        data.put("failcount", counts - count);
                    }
                    if ("2".equals(type)) {//更新
                        JSONArray dataArray = JSONArray.parseArray(dataList);
                        int counts = dataArray.size();
                        int count = 0;
                        for (int i = 0; i < dataArray.size(); i++) {
                            Map<String, Object> map = new HashMap<>();
                            JSONObject jsonObject = dataArray.getJSONObject(i);
                            String sjbm = Util.null2String(jsonObject.getString("sjbm"));
                            if (!StringUtil.isEmpty(sjbm)) {
                                for (Object key : jsonObject.keySet()) {
                                    map.put(Util.null2String(key), jsonObject.get(key));
                                }

                                String dataid = crmUtil.isExistData(sjbm, tablename);
                                if (!StringUtil.isEmpty(dataid)) {
                                    boolean flag = crmUtil.updateModelData(tablename, dataid, map);
                                    if (flag) {
                                        count++;
                                    }
                                }

                            }

                        }
                        data.put("totalcount", counts);
                        data.put("succcount", count);
                        data.put("failcount", counts - count);
                    }

                }
            }
        } catch (Exception e) {
            data.put("error", "程序内部异常");
            code = "SYSTEM_INNER_ERROR";
            message = "程序内部异常";
        }
        apidatas.put("code", code);
        apidatas.put("data", data);
        apidatas.put("message", message);
        writeLog(">>>>>saveBOInfoData>>>apidatas=" + apidatas.toString());
        return JSONObject.toJSONString(apidatas);
    }


}
